<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <!-- <el-form-item label="设备编号" prop="equipmentNo">
        <el-input v-model="queryParams.equipmentNo" placeholder="请输入设备编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input v-model="queryParams.equipmentName" placeholder="请输入设备名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="设备ip" prop="equipmentIp">
        <el-input v-model="queryParams.equipmentIp" placeholder="请输入设备ip" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="wifi名称" prop="wifiName">
        <el-input v-model="queryParams.wifiName" placeholder="请输入wifi名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="wifi密码" prop="wifiPassword">
        <el-input v-model="queryParams.wifiPassword" placeholder="请输入wifi密码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->

      <!-- <el-form-item label="数采盒子序列号" prop="serialNo">
        <el-input v-model="queryParams.serialNo" placeholder="请输入数采盒子序列号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="plc机架号" prop="rack">
        <el-input v-model="queryParams.rack" placeholder="请输入plc机架号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="plc插槽号" prop="slot">
        <el-input v-model="queryParams.slot" placeholder="请输入plc插槽号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="数采周期(ms)" prop="scadaInterval">
        <el-input v-model="queryParams.scadaInterval" placeholder="请输入数采周期(ms)" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="混凝土容量" prop="capacity">
        <el-input v-model="queryParams.capacity" placeholder="请输入混凝土容量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="queryParams.remarks" placeholder="请输入备注" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人id" prop="creatorId">
        <el-input v-model="queryParams.creatorId" placeholder="请输入创建人id" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人姓名" prop="creatorName">
        <el-input v-model="queryParams.creatorName" placeholder="请输入创建人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最后修改人id" prop="lastUpdaterId">
        <el-input v-model="queryParams.lastUpdaterId" placeholder="请输入最后修改人id" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最后修改人姓名" prop="lastUpdaterName">
        <el-input v-model="queryParams.lastUpdaterName" placeholder="请输入最后修改人姓名" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="最后修改时间" prop="lastUpdateTime">
        <el-date-picker clearable v-model="queryParams.lastUpdateTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择最后修改时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['pile:pilingMachine:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['pile:pilingMachine:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pile:pilingMachine:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['pile:pilingMachine:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pilingMachineList" @selection-change="handleSelectionChange"
      :row-class-name="tableRowClassName">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="设备编号" align="center" prop="equipmentNo" /> -->
      <el-table-column label="设备名称" align="center" prop="equipmentName" />
      <el-table-column label="设备ip" align="center" prop="equipmentIp" />
      <!-- <el-table-column label="wifi名称" align="center" prop="wifiName" />
      <el-table-column label="wifi密码" align="center" prop="wifiPassword" /> -->

      <el-table-column label="数采盒子序列号" align="center" prop="serialNo" />
      <!-- <el-table-column label="plc机架号" align="center" prop="rack" /> -->
      <!-- <el-table-column label="plc插槽号" align="center" prop="slot" /> -->
      <!-- <el-table-column label="数采返回类型 0 原始值 1 处理值 2 处理值+原始值" align="center" prop="rtnType" /> -->
      <el-table-column label="数采周期(ms)" align="center" prop="scadaInterval" />
      <el-table-column label="混凝土容量" align="center" prop="capacity" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="设备状态" width="130" align="center" prop="status">
        <template slot-scope="scope">
          <span :class="getDeviceStatusClass(scope.row.status)">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="创建人id" align="center" prop="creatorId" /> -->
      <!-- <el-table-column label="创建人姓名" align="center" prop="creatorName" /> -->
      <!-- <el-table-column label="最后修改人id" align="center" prop="lastUpdaterId" /> -->
      <!-- <el-table-column label="最后修改人姓名" align="center" prop="lastUpdaterName" /> -->
      <!-- <el-table-column label="最后修改时间" align="center" prop="lastUpdateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateTime) }}</span>
        </template>
</el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pile:pilingMachine:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pile:pilingMachine:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改桩机设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="设备编号" prop="equipmentNo">
          <el-input v-model="form.equipmentNo" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备ip" prop="equipmentIp">
          <el-input v-model="form.equipmentIp" placeholder="请输入设备ip" />
        </el-form-item>
        <!-- <el-form-item label="wifi名称" prop="wifiName">
          <el-input v-model="form.wifiName" placeholder="请输入wifi名称" />
        </el-form-item>
        <el-form-item label="wifi密码" prop="wifiPassword">
          <el-input v-model="form.wifiPassword" placeholder="请输入wifi密码" />
        </el-form-item> -->
        <el-form-item label="数采盒子序列号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入数采盒子序列号" />
        </el-form-item>
        <el-form-item label="plc机架号" prop="rack">
          <el-input v-model="form.rack" placeholder="请输入plc机架号" />
        </el-form-item>
        <el-form-item label="plc插槽号" prop="slot">
          <el-input v-model="form.slot" placeholder="请输入plc插槽号" />
        </el-form-item>
        <el-form-item label="数采周期(ms)" prop="scadaInterval">
          <el-input v-model="form.scadaInterval" placeholder="请输入数采周期(ms)" />
        </el-form-item>
        <el-form-item label="混凝土容量" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入混凝土容量" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建人id" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入创建人id" />
        </el-form-item>
        <el-form-item label="创建人姓名" prop="creatorName">
          <el-input v-model="form.creatorName" placeholder="请输入创建人姓名" />
        </el-form-item>
        <el-form-item label="最后修改人id" prop="lastUpdaterId">
          <el-input v-model="form.lastUpdaterId" placeholder="请输入最后修改人id" />
        </el-form-item>
        <el-form-item label="最后修改人姓名" prop="lastUpdaterName">
          <el-input v-model="form.lastUpdaterName" placeholder="请输入最后修改人姓名" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastUpdateTime">
          <el-date-picker clearable v-model="form.lastUpdateTime" type="date" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPilingMachine, getPilingMachine, delPilingMachine, addPilingMachine, updatePilingMachine } from "@/api/pile/pilingMachine";

export default {
  name: "PilingMachine",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 桩机设备表格数据
      pilingMachineList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentNo: null,
        equipmentName: null,
        equipmentIp: null,
        wifiName: null,
        wifiPassword: null,
        status: null,
        serialNo: null,
        rack: null,
        slot: null,
        rtnType: null,
        scadaInterval: null,
        capacity: null,
        remarks: null,
        creatorId: null,
        creatorName: null,
        lastUpdaterId: null,
        lastUpdaterName: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        equipmentNo: [
          { required: true, message: "设备编号不能为空", trigger: "blur" }
        ],
        equipmentName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        equipmentIp: [
          { required: true, message: "设备ip不能为空", trigger: "blur" }
        ],
        wifiName: [
          { required: true, message: "wifi名称不能为空", trigger: "blur" }
        ],
        wifiPassword: [
          { required: true, message: "wifi密码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "设备状态不能为空", trigger: "blur" }
        ],
        serialNo: [
          { required: true, message: "数采盒子序列号不能为空", trigger: "blur" }
        ],
        rack: [
          { required: true, message: "plc机架号不能为空", trigger: "blur" }
        ],
        slot: [
          { required: true, message: "plc插槽号不能为空", trigger: "blur" }
        ],
        rtnType: [
          { required: true, message: "数采返回类型 0 原始值 1 处理值 2 处理值+原始值不能为空", trigger: "change" }
        ],
        scadaInterval: [
          { required: true, message: "数采周期(ms)不能为空", trigger: "blur" }
        ],
        capacity: [
          { required: true, message: "混凝土容量不能为空", trigger: "blur" }
        ],
        remarks: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        creatorId: [
          { required: true, message: "创建人id不能为空", trigger: "blur" }
        ],
        creatorName: [
          { required: true, message: "创建人姓名不能为空", trigger: "blur" }
        ],
        lastUpdaterId: [
          { required: true, message: "最后修改人id不能为空", trigger: "blur" }
        ],
        lastUpdaterName: [
          { required: true, message: "最后修改人姓名不能为空", trigger: "blur" }
        ],
        lastUpdateTime: [
          { required: true, message: "最后修改时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 获取设备状态样式类 */
    getDeviceStatusClass(status) {
      if (!status) return 'status';

      const statusMap = {
        '在线': 'status online',
        '使用': 'status in-use',
        '闲置': 'status idle',
        '离线': 'status offline'
      };

      return statusMap[status] || 'status';
    },
    /** 表格行样式类名 */
    tableRowClassName({ row, rowIndex }) {
      return 'uniform-row';
    },
    /** 查询桩机设备列表 */
    getList() {
      this.loading = true;
      listPilingMachine(this.queryParams).then(response => {
        this.pilingMachineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        equipmentNo: null,
        equipmentName: null,
        equipmentIp: null,
        wifiName: null,
        wifiPassword: null,
        status: "0",
        serialNo: null,
        rack: null,
        slot: null,
        rtnType: null,
        scadaInterval: null,
        capacity: null,
        remarks: null,
        creatorId: null,
        creatorName: null,
        lastUpdaterId: null,
        lastUpdaterName: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加桩机设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPilingMachine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改桩机设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePilingMachine(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPilingMachine(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除桩机设备编号为"' + ids + '"的数据项？').then(function () {
        return delPilingMachine(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pile/pilingMachine/export', {
        ...this.queryParams
      }, `pilingMachine_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/* 表格行样式 - 去除单双行颜色 */
::v-deep .el-table__body tr.uniform-row {
  background-color: #fff;
}

::v-deep .el-table__body tr.uniform-row:hover {
  background-color: #f5f7fa;
}

/* 设备状态徽章样式 */
.status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.3px;
  border: 1px solid transparent;
  background: linear-gradient(#ffffff, #ffffff) padding-box,
    linear-gradient(135deg, #e5e7eb, #e5e7eb) border-box;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  position: relative;
  overflow: hidden;
}

.status::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  animation: pulse 2s infinite;
}

.status.online {
  background: linear-gradient(#f0fdf4, #f0fdf4) padding-box,
    linear-gradient(135deg, #10b981, #059669) border-box;
  color: #047857;
}

.status.in-use {
  background: linear-gradient(#fef3c7, #fef3c7) padding-box,
    linear-gradient(135deg, #f59e0b, #d97706) border-box;
  color: #b45309;
}

.status.idle {
  background: linear-gradient(#f8fafc, #f8fafc) padding-box,
    linear-gradient(135deg, #94a3b8, #64748b) border-box;
  color: #475569;
}

.status.offline {
  background: linear-gradient(#fef2f2, #fef2f2) padding-box,
    linear-gradient(135deg, #ef4444, #dc2626) border-box;
  color: #b91c1c;
}

.status.idle::before,
.status.offline::before {
  animation: none;
}

.status.idle::before {
  background: #64748b;
}

.status.offline::before {
  background: #dc2626;
}

.status.online::before {
  background: #10b981;
}

.status.in-use::before {
  background: #f59e0b;
}

@keyframes pulse {
  0% {
    opacity: 1;
    transform: scale(1);
  }

  50% {
    opacity: 0.7;
    transform: scale(1.1);
  }

  100% {
    opacity: 1;
    transform: scale(1);
  }
}
</style>
